package tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
class UserConfig implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int level;//难度
	String pokerBack;//牌背景
	String tableBack;//牌桌背景
	UserConfig(int level,String pokerBack,String tableBack) {
		// TODO Auto-generated constructor stub
		this.level=level;
		this.pokerBack=pokerBack;
		this.tableBack=tableBack;
	}
	public String toString(){
		return new StringBuffer().append(level).append(pokerBack)
.append(tableBack).toString();
	}
}
public class ReadConfig {
	private UserConfig currentUserConfig;
	public ReadConfig() {
		// TODO Auto-generated constructor stub
		currentUserConfig=readUserConfig();
	}
	/**获取配置文件路径
	 * 
	 * @return
	 */
	private String getConfigPath(){
		String res="file:";
		try {
			res=URLDecoder.decode(ReadConfig.class.getResource("/").toString(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new StringBuffer().append(res.substring(5,res.length())).append("config/").toString();
	}
	/**写出用户配置
	 * 
	 * @param userConfig 用户配置
	 */
	private void writeUserConfig(UserConfig userConfig){
		File file=new File(getConfigPath()+"UserConfig.dat");//建立文件
		try{
		    if (!file.exists()) file.createNewFile() ;
		    FileOutputStream fos = new FileOutputStream(file) ;
		    ObjectOutputStream oos = new ObjectOutputStream(fos) ;
		    oos.writeObject(userConfig);
		    oos.close();
		    fos.close();
		}catch (IOException e){
		    e.printStackTrace();
		}
	}
	/**读取用户配置
	 * 
	 * @return
	 */
	private UserConfig readUserConfig(){
		UserConfig userConfig=null;
		try{
		    FileInputStream fis = new FileInputStream(getConfigPath()+"UserConfig.dat") ;
		    ObjectInputStream ois = new ObjectInputStream(fis) ;
		    userConfig = (UserConfig)ois.readObject() ;
		    //System.out.println(tc);
		    ois.close();
		    fis.close();
		}catch (Exception e){
		    e.printStackTrace();
		    userConfig=new UserConfig(1, "哈利奎因","背景1");
		}
		return userConfig;
	}
	/**获取默认难度
	 * 
	 * @return
	 */
	public int getDefaultLevel(){
		return currentUserConfig.level;
	}
	/**修改默认难度
	 * 
	 * @param level 难度
	 */
	public void setDefaultLevel(int level){
		currentUserConfig.level=level;
		writeUserConfig(currentUserConfig);
	}
	/**获取当前扑克背景
	 * 
	 * @return
	 */
	public String getDefaultPokerBack(){
		return currentUserConfig.pokerBack;
	}
	/**设置扑克背景
	 * 
	 * @param pokerBack
	 */
	public void setDefaultPokerBack(String pokerBack){
		currentUserConfig.pokerBack=pokerBack;
		writeUserConfig(currentUserConfig);
	}
	/**获取当前桌面背景
	 * 
	 * @return
	 */
	public String getDefaultTableBack(){
		return currentUserConfig.tableBack;
	}
	/**设置扑克背景
	 * 
	 * @param pokerBack
	 */
	public void setDefaultTableBack(String tableBack){
		currentUserConfig.tableBack=tableBack;
		writeUserConfig(currentUserConfig);
	}
	/**读取背景配置文件
	 * 
	 * @param filename 文件名
	 * @return
	 */
	private Map<String,String> readBackFile(String filename){
		Map<String,String> res=new HashMap<>();
		File file=new File(filename);
		try{
			FileReader reader=new FileReader(file);
			BufferedReader buffer=new BufferedReader(reader);
			String curS=null;
			while((curS=buffer.readLine())!=null){
				String temp[]=curS.split("\t");
				res.put(temp[0], temp[1]);
			}
			buffer.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return res;
	}
	/**读取扑克背景
	 * 
	 * @return
	 */
	public Map<String,String> readPokerBack(){
		return readBackFile(getConfigPath()+"pokerBackConfig.txt");
	}
	/**读取牌桌背景
	 * 
	 * @return
	 */
	public Map<String,String> readTableBack(){
		return readBackFile(getConfigPath()+"tableBackConfig.txt");
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//new ReadConfig().writeUserConfig(1, "哈利奎因", "背景1");
		//System.out.println(new ReadConfig().readUserConfig());
		System.out.println(new ReadConfig().readTableBack());
	}

}
